﻿using Service.Entities;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
   public class BaseService<T> where T:BaseEntity
    {
        private OAContext db;

        public BaseService(OAContext db)
        {
            this.db = db;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Add(T entity)
        {
            db.Entry<T>(entity).State = EntityState.Added;
            return db.SaveChanges()>0;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(T entity)
        {
            db.Set<T>().Attach(entity);
            db.Entry<T>(entity).State = EntityState.Modified;
            return db.SaveChanges() > 0;
        }

        /// <summary>
        /// 删除(软删除==修改)
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Del(T entity)
        {
            db.Set<T>().Attach(entity);
            db.Entry<T>(entity).State = EntityState.Modified;
            return db.SaveChanges() > 0;
        }

        /// <summary>
        /// 模糊查询
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public IQueryable<T> Select(Expression<Func<T, bool>> whereLambda)
        {
            return db.Set<T>().Where(whereLambda);
        }
        /// <summary>
        /// 单表数据查询
        /// </summary>
        /// <param name="whereLambda"></param>
        /// <returns></returns>
        public T Get(Expression<Func<T,bool>> whereLambda)
        {
            return Select(whereLambda).FirstOrDefault();  
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="whereLambda"></param>
        /// <param name="orderBy"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public IQueryable<T> GetSelect<TKey>(Expression<Func<T, bool>> whereLambda, Expression<Func<T, bool>> orderBy, bool isAsc = true)
        {
            if (isAsc)
            {
                return Select(whereLambda).OrderBy(orderBy);
            }
            else
            {
                return Select(whereLambda).OrderByDescending(orderBy);
            }
        }

        /// <summary>
        /// 分页的封装方法
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <param name="start"></param>
        /// <param name="size"></param>
        /// <param name="rowCount"></param>
        /// <param name="whereLambda"></param>
        /// <param name="orderBy"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public IQueryable<T> PageSizeSelect<TKey>(int start, int size, ref int rowCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, TKey>> orderBy, bool isAsc = true)
        {
            rowCount = Select(whereLambda).Count();
            start = start < 1 ? 0 : start;
            if (isAsc)
            {
                return Select(whereLambda).OrderBy(orderBy).Skip(start).Take(size);
            }
            else
            {
                return Select(whereLambda).OrderByDescending(orderBy).Skip(start).Take(size);
            }

        }

       
   }
}
